package queue;

//                                          模拟实现队列
public class MyQueue {
    static class Node {
        public String val;
        public Node next;
        public Node(String val){
            this.val = val;
            next = null;
        }
    }
    public Node head = null;
    public Node tail = null;

    public void offer(String val){
        Node newNode = new Node(val);
        if (head == null){
            head = newNode;
            tail = newNode;
            return;
        }
        tail.next = newNode;
        tail = newNode;
    }
    public String poll(){
        if (head == null){
            return null;
        }
        String val = head.val;
        head = head.next;
        if (head == null){
            tail = null;
        }
        return val;
    }
    public String peek(){
        if (head == null){
            return null;
        }
        return head.val;
    }

    public boolean isEmpty(){
        return head == null;
    }
    public int size(){
        int size = 0;
        for (Node cur = head; cur != null; cur = cur.next){
            size++;
        }
        return size;
    }
    public String toString(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (Node cur = head; cur != null; cur = cur.next){
            stringBuilder.append(cur.val);
            if (cur.next != null){
                stringBuilder.append(", ");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
